package cn.com.weixinplatform.servlet;

import java.io.IOException;
import java.io.InputStream;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;

public class WeixinRequestServlet extends HttpServlet {

	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("----------get---------------");
		String signature = req.getParameter("signature");
		String timestamp = req.getParameter("timestamp");
		String nonce = req.getParameter("nonce");
		String echostr = req.getParameter("echostr");
		System.out.println("signature:" + signature);
		System.out.println("timestamp:" + timestamp);
		System.out.println("nonce:" + nonce);
		System.out.println("echostr:" + echostr);
		// String str = nonce + timestamp + "123456";
		// System.out.println(str);
		// String digest = new SHA1().getDigestOfString(str.getBytes());
		// System.out.println(digest);
		resp.getWriter().write(echostr);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		System.out.println("----------post---------------");
		InputStream is = req.getInputStream();

		byte b[] = new byte[1024];
		int len = 0;
		int temp = 0; // 所有读取的内容都使用temp接收
		while ((temp = is.read()) != -1) { // 当没有读取完时，继续读取
			b[len] = (byte) temp;
			len++;
		}
		is.close();
		String requestStr = new String(b, 0, len);
		String content = null;
		String toUser = null;
		String fromUser = null;
		try {
			Document doc = DocumentHelper.parseText(requestStr);
			Element ele = (Element) doc.selectSingleNode("/xml/Content");
			System.out.println(ele.getText());
			content = ele.getText();
			Element eleToUser = (Element) doc
					.selectSingleNode("/xml/ToUserName");
			Element eleFromUser = (Element) doc
					.selectSingleNode("/xml/FromUserName");
			toUser = eleToUser.getText();
			fromUser = eleFromUser.getText();
		} catch (DocumentException e) {
			e.printStackTrace();
		}
		if (content != null && content.indexOf("1") > -1) {
			String responseStr = "<xml>";
			responseStr += "<ToUserName><![CDATA[" + fromUser
					+ "]]></ToUserName>";
			responseStr += "<FromUserName><![CDATA[" + toUser
					+ "]]></FromUserName>";
			responseStr += "<CreateTime>" + System.currentTimeMillis()
					+ "</CreateTime>";
			responseStr += "<MsgType><![CDATA[text]]></MsgType>";
			responseStr += "<Content><![CDATA[访问成功]]></Content>";
			responseStr += "<FuncFlag>0</FuncFlag>";
			responseStr += "</xml>";
			resp.setCharacterEncoding("UTF-8");
			resp.getWriter().write(responseStr);
		} else if (content != null && content.indexOf("2") > -1) {
			String responseStr = "<xml>";
			responseStr += "<ToUserName><![CDATA[" + fromUser
					+ "]]></ToUserName>";
			responseStr += "<FromUserName><![CDATA[" + toUser
					+ "]]></FromUserName>";
			responseStr += "<CreateTime>" + System.currentTimeMillis()
					+ "</CreateTime>";
			responseStr += "<MsgType><![CDATA[news]]></MsgType>";
			responseStr += "<Content><![CDATA[]]></Content>";

			responseStr += "<ArticleCount>2</ArticleCount>";

			responseStr += "<Articles>";
			responseStr += "<item>";
			responseStr += "<Title><![CDATA[图片链接1]]></Title>";
			responseStr += "<Discription><![CDATA[内容内容内容1]]></Discription>";
			responseStr += "<PicUrl><![CDATA[http://m1.img.libdd.com/farm5/2012/1113/15/C19E7B37B1C42CF778C9EE4ED05DFE4188E6EE53DC014_160_120.GIF]]></PicUrl>";
			responseStr += "<Url><![http://m1.img.libdd.com/farm5/2012/1113/15/C19E7B37B1C42CF778C9EE4ED05DFE4188E6EE53DC014_160_120.GIF]]></Url>";
			responseStr += "</item>";

			responseStr += "<item>";
			responseStr += "<Title><![CDATA[图片链接2]]></Title>";
			responseStr += "<Discription><![CDATA[内容内容内容2]]></Discription>";
			responseStr += "<PicUrl><![CDATA[http://ww1.sinaimg.cn/bmiddle/a5f7ac80jw1dytcz0beojj.jpg]]></PicUrl>";
			responseStr += "<Url><![CDATA[http://ww1.sinaimg.cn/bmiddle/a5f7ac80jw1dytcz0beojj.jpg]]></Url>";
			responseStr += "</item>";

			responseStr += "</Articles>";
			responseStr += "<FuncFlag>1</FuncFlag>";
			responseStr += "</xml>";
			resp.setCharacterEncoding("UTF-8");
			resp.getWriter().write(responseStr);
		} else {
			String responseStr = "<xml>";
			responseStr += "<ToUserName><![CDATA[" + fromUser
					+ "]]></ToUserName>";
			responseStr += "<FromUserName><![CDATA[" + toUser
					+ "]]></FromUserName>";
			responseStr += "<CreateTime>" + System.currentTimeMillis()
					+ "</CreateTime>";
			responseStr += "<MsgType><![CDATA[text]]></MsgType>";
			responseStr += "<Content><![CDATA[没有关键字]]></Content>";
			responseStr += "<FuncFlag>0</FuncFlag>";
			responseStr += "</xml>";
			resp.setCharacterEncoding("UTF-8");
			resp.getWriter().write(responseStr);
		}

	}

}
